package com.lily.aidouzixun;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.lily.aidouzixun.activity.MainActivity;
import com.lily.aidouzixun.util.MySharedPreferences;


/**
 * 启动页面
 */
public class SplashActivity  extends Activity{
    private static final String TAG = "SplashActivity";

    private boolean isFirstIn = false;

    private static final int GO_HOME = 1;

    private static final int SPLASH_DELAY_MILLIS = 1500;    //延迟3s
    private boolean isClick = false;    //是否点击了图片
    private boolean isTime = false;    //到时间了
    private ImageView ivSplash;

    /**
     * 跳转到不同页面
     */
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME :
                    if (!isClick) {
                        isTime = true;
                        goHome();
                    }
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.splash);
        findViews();
        setListeners();
        init();
    }

    private void findViews() {
        ivSplash = (ImageView) this.findViewById(R.id.iv_splash);

    }

    private void  setListeners() {
        ivSplash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isClick = true;
                if (!isTime) {
                    goHome();
                }
            }
        });
    }

    /**
     * 初始化方法
     */
    private void init() {
        //是否是首次登陆
        isFirstIn = MySharedPreferences.getInstance().getGuided(SplashActivity.this);
        if(isFirstIn) { //首次进入，到引导页
            goGuide();
        } else{ //不是首次进入，延迟进入主界面
            mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
        }
    }

    /**
     * 跳到主页
     */
    private void goHome() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * 跳到引导页
     */
    private void goGuide() {
        //设置已经引导过了
        MySharedPreferences.getInstance().setGuided(SplashActivity.this);
        Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
        startActivity(intent);
        finish();
    }
}
